package com.database.controller;

import com.database.bean.Historys;
import com.database.service.HistorysService;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@RestController
public class HistorysController {
    private Long uid_final;
    final
    HistorysService historysService;

    public HistorysController(HistorysService historysService) {
        this.historysService = historysService;
    }
    @PostMapping(value = "/historys")
    public List<Historys> getHistoryByUid(HttpServletRequest request,
                                          @RequestParam(value = "sign", required = false, defaultValue = "0") Long sign,
                                          @RequestParam(value = "uid", required = false, defaultValue = "0") Long uid,
                                          @RequestParam(value = "type", required = false, defaultValue = "0") String type,
                                          @RequestParam(value = "vid",required = false,defaultValue = "0") String vid,
                                          @RequestParam(value = "rate",required = false,defaultValue = "0") Float rate
    ){
        HttpSession session = request.getSession();
        Long uidSession = (Long)session.getAttribute("uid");
        if(uidSession != null){
            this.uid_final = uidSession;
        }else{
            this.uid_final = uid;
        }
        if(this.uid_final == 0 || type == "0" || sign != 0){
            return new ArrayList<Historys>();
        }

        return historysService.getHistorysByUid(uid,type);
    }
}
